package com.ww.seckill.utils;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Component;

/**
 * @author ww
 */
@Component
public class MD5Util {

    private static final String salt = "hexiangdong";

    public static String md5(String src) {
        return DigestUtils.md5Hex(src);
    }

    /**
     * 前端MD5加密
     *
     * @param inputPass  前端字串
     * @return
     */
    public static String inputPassToFromPass(String inputPass) {
        String str = "" + salt.charAt(0) + salt.charAt(1) + inputPass + salt.charAt(2) + salt.charAt(3);
        return md5(str);
    }

    /**
     * 后端二次加密
     *
     * @param formPass  前端加密后字串
     * @param salt  后端配置盐
     * @return
     */
    public static String formPassToDBPass(String formPass, String salt) {
        String str = salt.charAt(0) + salt.charAt(1) + formPass + salt.charAt(2) + salt.charAt(3);
        return md5(str);
    }

    /**
     * 前端两次加密，最终加密字串
     *
     * @param inputPass
     * @param salt
     * @return
     */
    public static String inputPassDBPass(String inputPass, String salt) {
        String fromPass = inputPassToFromPass(inputPass);
        String dbPass = formPassToDBPass(fromPass, salt);
        return dbPass;
    }


    public static void main(String[] args) {
        System.out.println(inputPassToFromPass("123456"));  // cf6473ca0856077cdd4fb2f9c4dfaeae
        System.out.println(formPassToDBPass("cf6473ca0856077cdd4fb2f9c4dfaeae", salt));

        // 二次加密后密码
        System.out.println(inputPassDBPass("123456", salt));
    }

}
